home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 3.2 KB | 152 lines | [TEXT/CWIE] |
- package au.com.peter.libraries;
-
- import java.io.*;
- import java.net.Socket;
- import java.util.Vector;
-
- public class NetClient extends TCPClient {
-
- public boolean debug = true;
- private StringBuffer line_buffer = new StringBuffer( 32 );
-
- public String ReadLine() {
- String line;
-
- try {
- int c;
-
- line_buffer.setLength( 0 );
- while ( true ) {
- c = input.read();
- if ( c == -1 || c == '\n' || c == '\r' ) {
- break;
- }
- line_buffer.append( (char) c );
- }
- if ( c == '\r' ) {
- c = input.read(); // skip the \n
- }
-
- line = line_buffer.toString();
- if ( debug ) {
- System.out.println( ">" + ObscureCommand( line ) );
- }
- } catch( IOException e ) {
- line = null;
- }
-
- return line;
- }
-
- public static int ResponseCode( String line ) {
- int code;
-
- if ( line.length() < 3 ) {
- code = -1;
- } else {
- try {
- code = Integer.parseInt( line.substring( 0, 3 ) );
- } catch ( NumberFormatException e ) {
- code = -1;
- }
- }
-
- return code;
- }
-
- public static int ResponseHighCode( int code ) {
- if ( code < 100 || code > 599 ) {
- return -1;
- } else {
- return code / 100;
- }
- }
-
- public String ReadResponse() {
- String line = ReadLine();
- int code = ResponseCode( line );
- if ( code >= 0 && line.length() >= 4 && line.charAt( 3 ) == '-' ) {
- String temp;
- do {
- temp = ReadLine();
- } while ( temp.length() < 4 || temp.charAt( 3 ) != ' ' || ResponseCode( temp ) != code );
- }
-
- return line;
- }
-
- public int ReadResponseCode() {
- return ResponseCode( ReadResponse() );
- }
-
- public int ReadResponseHighCode() {
- return ResponseHighCode( ResponseCode( ReadResponse() ) );
- }
-
- public void ReadResponseCheck() throws IOException {
- if ( ReadResponseHighCode() != 2 ) {
- throw new NetClientException( "response check failed" );
- }
- }
-
- public Vector ReadResponses() {
- Vector lines = new Vector();
-
- String line = ReadLine();
- lines.addElement( line );
- int code = ResponseCode( line );
- if ( code >= 0 && line.length() >= 4 && line.charAt( 3 ) == '-' ) {
- String temp;
- do {
- temp = ReadLine();
- lines.addElement( line );
- } while ( temp.length() < 4 || temp.charAt( 3 ) != ' ' || ResponseCode( temp ) != code );
- }
-
- return lines;
- }
-
- public void SendLineNoFlush( String line ) throws IOException {
- int len = line.length();
- for ( int i = 0; i < len; i++ ) {
- output.write( line.charAt(i) );
- }
- output.write( '\r' );
- output.write( '\n' );
- }
-
- public void SendLine( String line ) throws IOException {
- SendLineNoFlush( line );
- output.flush();
- }
-
- public int SendCommandHighCode( String command ) throws IOException {
- SendLine( command );
- if ( debug ) {
- System.out.println( "<" + ObscureCommand( command ) );
- }
- return ReadResponseHighCode();
- }
-
- public void SendCommandCheck( String command ) throws IOException {
- if ( SendCommandHighCode( command ) != 2 ) {
- throw new NetClientException( "command '"+ ObscureCommand( command ) + "' failed" );
- }
- }
-
- public static String ObscureCommand( String command ) {
- if ( command.regionMatches( true, 0, "PASS", 0, 4 ) ) {
- command = "PASS ******";
- }
- return command;
- }
-
- }
-
- class NetClientException extends TCPClientException {
- public NetClientException( String s ) {
- super( s );
- }
- }
-
-